﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace NB_Controller_Test
{
    class SystemConfig
    {
        public class ConfigItem
        {
            public string strName = "";
            public string strValue = "";
        }

        protected const string XML_NODE_PROJECT = "SystemConfig";
        protected const string XML_DEVICE = "ConfigItem";

        protected const string XML_NAME = "Name";
        protected const string XML_DETAIL = "Value";

        public string PathName;

        public List<ConfigItem> systemConfiList = new List<ConfigItem>();

        public SystemConfig()
        {
            PathName = System.Windows.Forms.Application.StartupPath + "\\SystemConfi.xml";
            Open();
        }

        public bool Open()
        {
            bool bRet = true;

            try
            {
                XmlDocument _xmldoc = new XmlDocument();
                _xmldoc.Load(PathName);

                XmlNode nodePage = _xmldoc.SelectSingleNode(XML_NODE_PROJECT);
                if (nodePage != null)
                {
                    foreach (XmlNode _node in nodePage.ChildNodes)
                    {
                        if (_node.NodeType != XmlNodeType.Element)
                            continue;

                        XmlElement _elm = (XmlElement)_node;

                        //================================
                        if (_elm.Name.Equals(XML_DEVICE))
                        {
                            ParseXML(_elm);
                        }
                    }
                }
            }
            catch
            {
                //Save();
            }
            return bRet;
        }

        public bool Save()
        {
            if (File.Exists(PathName))
            {
                File.Delete(PathName);
            }

            XDocument xDoc = new XDocument();
            XElement xRoot = new XElement(XML_NODE_PROJECT);
            // 添加节点使用Add
            xDoc.Add(xRoot);

            int nCount = systemConfiList.Count;
            for (int i = 0; i < nCount; i++)
            {
                ConfigItem sendCmd = systemConfiList[i];

                XElement xDevItem = new XElement(XML_DEVICE);
                xRoot.Add(xDevItem);

                XElement xDevNameItem = new XElement(XML_NAME);
                xDevNameItem.Value = sendCmd.strName;
                xDevItem.Add(xDevNameItem);

                XElement xDevDetailItem = new XElement(XML_DETAIL);
                xDevDetailItem.Value = sendCmd.strValue;
                xDevItem.Add(xDevDetailItem);
            }

            //保存该文档  
            xDoc.Save(PathName);

            return true;
        }

        ///*******************************************************************************
        /// <summary>
        /// 解析 近端秘钥列表
        /// </summary>
        /// <param name="_elm"></param>
        ///*******************************************************************************
        public void ParseXML(XmlElement _elm)
        {
            if (_elm.Name.Equals(XML_DEVICE))
            {
                ConfigItem sendCmd = new ConfigItem();

                foreach (XmlNode xmlNode in _elm.ChildNodes)
                {
                    if (xmlNode.NodeType != XmlNodeType.Element)
                        continue;
                    XmlElement xmlElm = (XmlElement)xmlNode;

                    if (xmlElm.Name.Equals(XML_NAME))
                    {
                        sendCmd.strName = xmlElm.InnerText;
                    }
                    else if (xmlElm.Name.Equals(XML_DETAIL))
                    {
                        sendCmd.strValue = xmlElm.InnerText;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("xmlElm.Name : " + xmlElm.Name + " is not save!");
                    }
                }

                systemConfiList.Add(sendCmd);
            }
        }

        public void AddNewConfigItem(ConfigItem item)
        {
            systemConfiList.Add(item);
        }

        public string GetValueByName(string strName)
        {
            foreach (ConfigItem item in systemConfiList)
            {
                if (item.strName == strName)
                {
                    return item.strValue;// = itemNew.strValue;
                }
            }

            return "";
        }

        public bool UpdateConfigItemValue(ConfigItem itemNew)
        {
            foreach(ConfigItem item in systemConfiList)
            {
                if(item.strName == itemNew.strName)
                {
                    item.strValue = itemNew.strValue;
                    return true;
                }
            }

            return false;
        }
    }
}
